<?php

// 主页

namespace app\mobile\controller;

use think\Cache;
use think\Session;
use think\Controller;
use app\mobile\model\Ad as AdModel;
use app\mobile\model\Limited as LimitedModel;
use app\mobile\model\ShopType as ShopTypeModel;
use app\mobile\model\GoodsType as GoodsTypeModel;
use app\mobile\model\Promotion as PromotionModel;
use app\mobile\model\ShopGoods as ShopGoodsModel;
use app\mobile\model\LimitedType as LimitedTypeModel;
use app\mobile\model\MemberBrowse as MemberBrowseModel;

class Index extends Controller
{

    /**
     * 商城主页
     * @param AdModel $ad
     * @param GoodsTypeModel $goodsType
     * @param LimitedModel $limited
     * @param PromotionModel $promotion
     * @param ShopTypeModel $shopType
     * @return mixed
     */
    public function index(AdModel $ad, GoodsTypeModel $goodsType, LimitedModel $limited, PromotionModel $promotion, ShopTypeModel $shopType)
    {
        $a = floor(bcmul(21.00,0.1,2));
        halt($a);
        // banner图
        $banner_list = $ad->where(['parenTid' => '1'])->field('linkUrl,picUrl,skip_type')->select();

        // 热点
        $hotSpot = $promotion->where('status', 1)->order(['sort' => 'desc', 'create_time' => 'desc'])->field('id,title')->select();

        // 限时抢购商品
        $limited_goods = $limited->with('LimitedType,ShopGoods')->limit(5)->select()->append(['current_time'])->toArray();

        // 限时抢购剩余时间
        $limitedType  = new LimitedTypeModel();
        // $end_time = (new LimitedTypeModel())->where('id', 2)->value('end_time');
        // $start_time = (new LimitedTypeModel())->where('id', 2)->value('start_time');
        $end_time = $limitedType->where('id', 2)->value('end_time');
        $start_time = $limitedType->where('id', 2)->value('start_time');
        if ($end_time<time()||$start_time>time()) {
            $limited_goods = " ";
        }
        // 首页热门市场
        $hot_list = $goodsType->where(['parenTid' => '0', 'recommend' => '1', 'status' => '1'])->field('id,title,picUrl')->order(['sort' => 'desc'])->limit(1)->select();

        // 热门市场正常推荐
        $type_list = $goodsType->where(['parenTid' => '0', 'hot_recommend' => '1', 'status' => '1'])->field('id,title,picUrl')->order(['sort' => 'desc'])->limit(8)->select();

        // 发现好店
        $find_shop = $shopType->limit('4')->select()->toArray();

        // 猜你喜欢
        $guess_like = self::guess_like();

        return $this->fetch('', [
            'mate_title'   => '红树湾商城',
            'banner_list'  => $banner_list,
            'hot_spot'     => $hotSpot,
            'limited_list' => $limited_goods,
            'end_time'     => strtotime($end_time),
            'hot_list'     => $hot_list,
            'type_list'    => $type_list,
            'find_shop'    => $find_shop,
            'guess_like'   => $guess_like
        ]);

    }


    /**
     * 猜你喜欢
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function guess_like()
    {
        // 实例化
        $shopGoods = new ShopGoodsModel;
        $memberBrowse = new MemberBrowseModel;
        $guess_like = [];

        //猜你喜欢
        $map['status'] = 1;

        // 开启推送
        if (Cache::get('web_commodity_audit') == 'Y') {
            $map['related'] = ['eq', 1];
            $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage')->limit('4')->order(['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'])->select();
        } else {
            // 未开启推送且未登录
            if (Session::has('member_id')) {

                $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage')->limit('4')->order(['rand()' => 'desc'])->select();
            } else {
                // 未开启推送且登录

                $brose = $memberBrowse->where('member_id', Session::get('member_id'))->field('goods_type')->order('create_time', 'desc')->limit(5)->select();
                // 如果浏览记录为空
                if (empty($brose)) {
                    $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage')->limit('4')->order(['rand()' => 'desc'])->select();
                } else {
                    $brose_str = implode(',', $brose);
                    $map['goods_id'] = ['in', $brose_str];
                    $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage')->limit('4')->order(['rand()' => 'desc'])->select();
                }
            }
        }

        return $guess_like;
    }



    public function sob()
    {
        return $this->fetch('');
    }
}